package com.pi.uc.service;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.pi.base.dto.result.respcode.user.UcResp;
import com.pi.base.enumerate.record.RecordState;
import com.pi.base.enumerate.user.Gender;
import com.pi.base.exception.ServiceException;
import com.pi.base.util.bean.ObjectTool;
import com.pi.uc.dao.entity.UcUserWxEntity;
import com.pi.uc.dao.mapper.UcUserWxMapper;
import com.pi.uc.dao.param.UcUserWxParam;

@Validated
@Service
public class UcUserWxService {
  @Autowired
  private UcUserWxMapper userWxMapper;
  
  public Long addUserWxInfoIfNotExist(
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId, 
      String wxUnionId, 
      @NotNull(message="UC_USER.USER_NOT_FOUND")Long userId,
      @NotNull(message="UC_USER.SOURCE_ID_EMPTY") Long sourceId){
    UcUserWxEntity entity = queryUserWxInfoIfExist(userId);
    if(null != entity){
      if( wxOpenId.equalsIgnoreCase(entity.getWxOpenid()) 
          || wxUnionId.equalsIgnoreCase(entity.getWxUnionid())){
        if(null == wxUnionId)
          wxUnionId = StringUtils.EMPTY;
        updateUserWxInfo(wxOpenId, wxUnionId, entity.getId());
      }
      return entity.getId();
    }
    return insertUserWxInfo(wxOpenId, wxUnionId, userId, sourceId);
  }
  
  public Long insertUserWxInfo(
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId, 
      String wxUnionId, 
      @NotNull(message="UC_USER.USER_NOT_FOUND")Long userId,
      @NotNull(message="UC_USER.SOURCE_ID_EMPTY") Long sourceId){
    UcUserWxEntity entity = new UcUserWxEntity();
    entity.setUserId(userId);
    entity.setSourceId(sourceId);
    entity.setWxOpenid(wxOpenId);
    entity.setWxUnionid(null != wxUnionId? wxUnionId : StringUtils.EMPTY);
    entity.setWxNickName(StringUtils.EMPTY);
    entity.setState(RecordState.STATE_NORMAL.getCode());
    entity.setWxAvatar(StringUtils.EMPTY);
    entity.setWxCity(StringUtils.EMPTY);
    entity.setWxCountry(StringUtils.EMPTY);
    entity.setWxLanguage(StringUtils.EMPTY);
    entity.setWxProvince(StringUtils.EMPTY);
    entity.setWxSex(Gender.UNKNOWN.getCode());
    userWxMapper.insert(entity);
    return entity.getId();
  }
  
  public void updateUserWxInfoByUserId(
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId, 
      @NotNull( message = "UC_USER.USER_WXINFO_LOST" ) String wxUnionId, 
      @NotNull(message="UC_USER.USER_NOT_FOUND")Long userId,
      @NotNull(message="UC_USER.SOURCE_ID_EMPTY") Long sourceId){
    UcUserWxEntity entity = queryUserWxInfo(userId);
    entity.setWxOpenid(wxOpenId);
    if(StringUtils.isNotBlank(wxUnionId))
      entity.setWxUnionid(wxUnionId);
    userWxMapper.updateById(entity);
  }
  
  public void updateUserWxInfo(
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId, 
      @NotNull( message = "UC_USER.USER_WXINFO_LOST" ) String wxUnionId, 
      @NotNull(message="UC_USER.USER_WXINFO_LOST")Long id){
    UcUserWxEntity entity = new UcUserWxEntity();
    entity.setId(id);
    entity.setWxOpenid(wxOpenId);
    entity.setWxUnionid(wxUnionId);
    userWxMapper.updateById(entity);
  }
  
  public UcUserWxEntity queryUserWxInfo(
      @NotNull(message="UC_USER.USER_NOT_FOUND")Long userId){
    UcUserWxEntity entity = queryUserWxInfoIfExist(userId);
    if(null == entity){
      throw new ServiceException(
          UcResp.RESP_USER_WXINFO_NOT_FOUND.getTag(),
          UcResp.RESP_USER_WXINFO_NOT_FOUND.getErrorCode());
    }
    return entity;
  }
  
  public UcUserWxEntity queryUserWxInfoIfExist( 
      @NotNull(message="UC_USER.USER_NOT_FOUND")Long userId){
//  查找用户微信是否存在
    UcUserWxParam param = new UcUserWxParam();
    param.setUserId(userId);
    List<UcUserWxEntity> userWxList = userWxMapper.findList(param);
    return ObjectTool.getOne(userWxList);
  }
  
  public UcUserWxEntity queryWxInfo(
      @NotNull(message="UC_USER.SOURCE_ID_EMPTY") Long sourceId,
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId,
      String wxUnionId){
    UcUserWxEntity entity = queryWxInfoIfExist(sourceId, wxOpenId, wxUnionId);
    if(null == entity){
      throw new ServiceException(
          UcResp.RESP_USER_WXINFO_NOT_FOUND.getTag(),
          UcResp.RESP_USER_WXINFO_NOT_FOUND.getErrorCode());
    }
    return entity;
  }
  
  public UcUserWxEntity queryWxInfoIfExist(
      @NotNull(message="UC_USER.SOURCE_ID_EMPTY") Long sourceId,
      @NotBlank( message = "UC_USER.USER_WXINFO_LOST" ) String wxOpenId,
      String wxUnionId){
    UcUserWxParam param = new UcUserWxParam();
    param.setSourceId(sourceId);
    param.setWxOpenid(wxOpenId);
    if(StringUtils.isNotBlank(wxUnionId)){
      param.setWxUnionid(wxUnionId);
    }
    List<UcUserWxEntity> userWxList = userWxMapper.findList(param);
    return ObjectTool.getOne(userWxList);
  }
}
